package cloud.provider.payment.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;

import cloud.provider.payment.service.PayService;
import com.xfanonymous.cloud.entities.Pay;
import com.xfanonymous.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;

/**
 * @auther xfanonymous
 * @create 2023-12-29 17:40
 *
 * 1. 服务端绑定Gateway Route路由，网关作为对外唯一接口
 * 2. Gateway Filter过滤器
 */
@RestController
public class PayGateWayController
{
    @Resource
    PayService payService;

    // TODO：1.Gateway Route路由
    @GetMapping(value = "/pay/gateway/get/{id}")
    public ResultData<Pay> getById(@PathVariable("id") Integer id)
    {
        Pay pay = payService.getById(id);
        return ResultData.success(pay);
    }

    @GetMapping(value = "/pay/gateway/info")
    public ResultData<String> getGatewayInfo()
    {
        return ResultData.success("gateway info test："+ IdUtil.simpleUUID());
    }

    // TODO: 2.Gateway Filter过滤器
    @GetMapping(value = "/pay/gateway/filter")
    public ResultData<String> getGatewayFilter(HttpServletRequest request)
    {
        String result = "";
        Enumeration<String> headers = request.getHeaderNames();
        // 打印请求头，不仅会获取到Servlet请求的Header，还可以获取到配置文件中添加的kv
        while(headers.hasMoreElements())
        {
            String headName = headers.nextElement();
            String headValue = request.getHeader(headName);
            System.out.println("请求头名: " + headName +"\t\t\t"+"请求头值: " + headValue);

            // 获取配置文件中AddRequestHeader添加的kv
            if (headName.equalsIgnoreCase("X-Request-xfanonymous1")
                    || headName.equalsIgnoreCase("X-Request-xfanonymous2")) {
                result = result + headName + "\t " + headValue +" ";
            }
        }
        System.out.println("=============================================");
        /** 请求参数配置
         *  添加请求参数 AddRequestParameter=customerId,9527001
         *  如果URL中本身有此参数，则URL优先级高，会打印 customerId: 999
         *  http://localhost:9527/pay/gateway/filter?customerId=999&customerName=fan
         */
        String customerId = request.getParameter("customerId");
        System.out.println("request Parameter customerId: "+customerId);

        // 请求参数配置，url?customerName=fan，RemoveRequestParameter后，打印结果为null
        String customerName = request.getParameter("customerName");
        System.out.println("request Parameter customerName: "+customerName);

        System.out.println("=============================================");

        // 获取配置文件中的kv
        return ResultData.success("getGatewayFilter 过滤器 test： "+result+" \t "+ DateUtil.now());
        /**
         * {
         *     "code": "200",
         *     "message": "success",
         *     "data": "getGatewayFilter 过滤器 test： x-request-xfanonymous1\t fanValue1 x-request-xfanonymous2\t fanValue2  \t 2024-03-21 14:31:30",
         *     "timestamp": 1711002690947
         * }
          */
    }
    /**
     * 请求头名: user-agent			请求头值: PostmanRuntime/7.29.0
     * 请求头名: accept               请求头值: *
     * 请求头名: postman-token			请求头值: 3ffe286f-6223-4291-81a3-3fee45909779
     * 请求头名: accept-encoding			请求头值: gzip, deflate, br
     * 请求头名: x-request-xfanonymous1			请求头值: fanValue1
     * 请求头名: x-request-xfanonymous2			请求头值: fanValue2
     * 请求头名: sec-fetch-mode			请求头值: Blue-updateByFan
     * 请求头名: forwarded			请求头值: proto=http;host="localhost:9527";for="[0:0:0:0:0:0:0:1]:11363"
     * 请求头名: x-forwarded-for			请求头值: 0:0:0:0:0:0:0:1
     * 请求头名: x-forwarded-proto			请求头值: http
     * 请求头名: x-forwarded-port			请求头值: 9527
     * 请求头名: x-forwarded-host			请求头值: localhost:9527
     * 请求头名: host			请求头值: 192.168.56.1:8001
     * 请求头名: content-length			请求头值: 0
     * =============================================
     * request Parameter customerId: 999
     * request Parameter customerName: null
     * =============================================
     * */
}